#include "..\headers\ListOf.h"

typedef struct hafmann {
  int wight;
  char Str;
};

// 冒泡排序
int sort(hafmann hafmanns[], int n) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n - i - 1; j++)
      if (hafmanns[j].wight > hafmanns[j + 1].wight) {
        hafmann temp = hafmanns[j];
        hafmanns[j] = hafmanns[j + 1];
        hafmanns[j + 1] = temp;
      }
  return OK;
}
// 计算哈夫曼值
int hafmanValue(hafmann hafmanns[], int n) {
  int head = 0;
  int Hafmann;
  while (head != n - 1) {
    int n1 = hafmanns[head].wight;
    hafmanns[head].wight = 0;
    int n2 = hafmanns[++head].wight;
    hafmanns[head].wight = 0;
    hafmanns[head].wight = n1 + n2;
    sort(hafmanns, n);
    Hafmann += n1 + n2;
  }
  return Hafmann;
}

int main() {
  // 输入编码频率
  int n = 0;
  scanf("%d", &n);
  hafmann hafmanns[n], temp[n];
  for (int i = 0; i < n; i++) {
    getchar();
    scanf("%c %d", &hafmanns[i].Str, &hafmanns[i].wight);
    temp[i] = hafmanns[i];
  }
  // 对输入的编码频率进行排序
  sort(hafmanns, n);
  // 计算哈夫曼值
  int Hafmann = hafmanValue(hafmanns, n);
  // 输入哈夫曼编码
  int m;
  scanf("%d", &m);
  for (int k = 0; k < m; k++) {
    int Ohafmanns = 0;
    char code[3000][66] = {0};
    char Str;
    for (int i = 0; i < n; i++) {
      getchar();
      scanf("%c %s", &Str, code[i]);
      Ohafmanns += (int)strlen(code[i]) * temp[i].wight;
    }

    if (Ohafmanns != Hafmann) {
      printf("No\n");
    } else if (Ohafmanns == Hafmann) {
      int flag = 1;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n && j != i; j++) {
          if (strlen(code[i]) <= strlen(code[j]) &&
              strncmp(code[i], code[j], strlen(code[i])) == 0) {
            flag = 0;
            break;
          } else if (strlen(code[i]) > strlen(code[j]) &&
                     strncmp(code[i], code[j], strlen(code[j])) == 0) {
            flag = 0;
            break;
          }
        }
        if (flag == 0) break;
      }
      if (flag == 1) {
        printf("Yes\n");
      } else {
        printf("No\n");
      }
    }
  }
  return 0;
}